package com.example.blogsystem_ssm.controller;

import com.example.blogsystem_ssm.model.Blog;
import com.example.blogsystem_ssm.model.User;
import com.example.blogsystem_ssm.service.BlogService;
import com.example.blogsystem_ssm.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class BlogController {
    @Autowired
    BlogService blogService;

    @RequestMapping(value = "/blog")
    public ResponseBodyMessage<List<Blog>> getBlogs(){
        List<Blog> list = blogService.getBlogs();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Blog b:list) {
            if(b.getContent().length()>50){
                b.setContent(b.getContent().substring(0,50));
            }
            b.setPostTime(b.getPostTime());
        }
        return new ResponseBodyMessage<>(0,"获取到了博客信息！",list);
    }
    @RequestMapping(value = "/getuserid")
    public User getuserid(HttpServletRequest request){
        return (User)request.getSession().getAttribute("User");
    }
}
